<?php
session_start();


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserClass
 *
 * @author Jo
 */
class UserClass {
    var $userID;            // This user's id
    var $admin;             // Is This user an admin?
    var $medarbeider;       // Is This user a medarbeider?


    // Constructor
    public function UserClass() {
        if(!isset ($_SESSION['uid'])){      // If the user is not logged in:
            $this->userID = -1;             // set default values to the
            $this->admin = false;           // variables:
            $this->medarbeider = false;
        }
        else{                               // If the user is logged in:
            $this->userID = $_SESSION['uid'];       // Set ID so we know the user is logged in.
            $row = $this->getAllUserData($this->userID); // Get the user-type.            
            $this->setUserType($row['type']);       // Set the proper restrictions to this user.
        }
    }

    public function isLoggedOn() {
        if ($this->userID > -1) {return true;} 
        else                    {return false;}
    }

    public function isAdmin() {
        if ($this->admin == true)   {return true;}
        else                        {return false;}
    }

    public function isMedarbeider() {
        if ($this->medarbeider == true) {return true;} 
        else                            {return false;}
    }

    // Log in user:
    public function doLogin($uid, $type) {
        $this->userID = $_SESSION['uid'] = $uid;    // Set ID and SESSION so we know the user is logged in.
        $this->setUserType($type);                  // Set proper restricitons.
    }

    // Log out user:
    public function logOut() {
        session_unset();            // Unset the current SESSION value.
        session_destroy();          // Destroy the SESSION.
        $this->userID = -1;         // Set default values to the variables:
        $this->admin = false;
        $this->medarbeider = false;
    }

    public function getUid() {
        return $this->userID;
    }

    // Returns all information in DB about a user.
    public function getAllUserData($userID){
        global $db;                                 // Make database usable.
        $user_query = 'SELECT * FROM users WHERE uid='. $userID;  // db query.
        $executor = $db->prepare($user_query);       // Prepare $user_query to be executed.
        $executor->execute();                        // Execute the db query.
        $row = $executor->fetch();                   // Fetch all data in array $row.
        return $row;                            // return array with data.
    }
    
    //Redirects the user to right place
    public function redirectUser(){
        if($this->isAdmin()) {
            header("Location: /adminViews/myAdminSite.php"); 
        } else if($this->isMedarbeider())
            header("Location: /Views/myEmpSite.php"); 
    }

    // Set the proper restrictions of this user.
    public function setUserType($type){        
        if ($type == 'administrator') {     // If user is an admin, let him see            
            $this->admin = true;            //  everything both the admin and the
            $this->medarbeider = true;      //  medarbeider sees.
        } else if ($type == 'employee') {// If the user is a medarbeider, let            
            $this->medarbeider = true;      //  him see everything a medarbeider sees.
        }
    }

    function getLoginForm() {
        // This function contains the display of both login and logout buttons
        $url_path = '/dev/nettbutikk/Functions/authFunctions.php';
        if (!$this->isLoggedOn()) { // If this user isnt logged on: Show login button:?>
            <form id="logon" method="post" action="<?php echo $url_path; ?>"><?php
            echo '<label for="username">E-post:</label><input type="email" name="username" required = "required" /><br>';
            echo '<label for="password">Passord:</label><input type="password" name="password" required = "required" /><br>';
            echo '<input name = "login" type = "submit" value = "logg inn" />';
            echo '<a href="/Views/registrer.php"><p>Registrer deg!</p></a>';
            ?></form><?php
        } else { // if this user is logged on: Show logoff button:?>
            <form id="logff" method="post" action="<?php echo $url_path; ?>">
            <input name = "logout" type = "submit" value = "Logg ut" />
            </form>
            <?php $userInfo = $this->getAllUserData($this->userID) ?>
            <h2>Velkommen <?php echo $userInfo['givenname'] . ' ' . $userInfo['surename']; ?></h2>
            <?php
        }
    }

}

$user = new UserClass ();
?>
